home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17853 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  80 lines

  1. Path: qualcomm.com!not-for-mail
  2. From: drew@qualcomm.com (Drew Eckhardt)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: INITIALIZATION of a MEMBER CLASS of a CLASS
  5. Date: 17 Apr 1996 14:19:18 -0600
  6. Organization: QUALCOMM, Incorporated; Boulder, CO, USA;
  7. Message-ID: <4l3jo6$jpl@qualcomm.com>
  8. References: <4l37o3$iug@risky.ecs.umass.edu>
  9. NNTP-Posting-Host: littlebear.qualcomm.com
  10.  
  11. In article <4l37o3$iug@risky.ecs.umass.edu>,
  12. Kiran K Toutireddy <ktoutire@chopin.ecs.umass.edu> wrote:
  13. >
  14. >
  15. >Hi,
  16. >    I have a question related to initializing the member classes inside a class data structure. The following example should explain the question.
  17.  
  18.  
  19.  
  20. >    I want to initialize the car_engine member to be Gas engine by default, instead of the default electric engine. How do I do that?
  21.  
  22. Initializer lists.
  23.  
  24. >    the following is some pseudo code of the above example
  25. >
  26. >    class Engine{
  27. >        nuts..bolts..pipes..rods...;
  28. >    public:
  29. >        Engine(){  make this an electric engine,....;}
  30. >        Engine(int Gas){ make it a gas engine..}
  31. >        Engine(float Deisel){ make it a deisel engine;}
  32. >    };
  33. >    
  34. >    class Car{
  35. >        Engine car_engine;
  36. >    public:
  37.            Car::Car() :
  38.           Engine (/* Some integer here */)
  39.            {
  40.           // Body of constructor here.
  41.            }
  42. >    };
  43. >
  44.  
  45. A few other related comments:
  46.  
  47. 1.  If you wish to specify multiple initializers, you use a comma
  48.     delimited list.  Ie
  49.  
  50.     foo::foo () : 
  51.       member1(x),
  52.       member2(member1)
  53.  
  54. 2.  Base classes with parameterized constructors can also be initialized
  55.     this way; although you use the class name instead of the member name.
  56.  
  57. 3.  Initializers are executed in the order of member declaration,  
  58.     not initializer ordering.  Ie, given 
  59.  
  60.        struct foo 
  61.        {
  62.        public:
  63.      foo (int a, int b) : x (a), y(b) {}
  64.        private:
  65.      int y, x;
  66.        }
  67.  
  68.     y would be initialized first.
  69.  
  70. >I tried initializing the car_engine by saying Engine car_engine(int Gas), but it gives a warning saying that ANSI c++ forbids such declarations. 
  71.  
  72. That would declare a function called car_engine taking an integer parameter
  73. and returning an Engine.
  74.  
  75.  
  76. -- 
  77. <a href="http://www.poohsticks.org/drew/">Home Page</a>
  78. Four boxes : soap, ballot, jury, ammo.  | Work: drew@Qualcomm.COM       
  79. Use in that order.                      | Play: drew@PoohSticks.ORG    
  80.